package com.homedroid.dal;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.homedroid.ui.List1;
import com.homedroid.ui.List2;

public class Parser {
	
	private String strmessage = null;
	private boolean version, message, amount,actual_amount, barcode, label, brand, content_, content_2, unit;
	private int auswahl = 0;
	
	public String checkUpdates(int versionint) throws IOException {
		String verxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<checkUpdates>"
					+ "<version>" + versionint	+ "</version>"
				+ "</checkUpdates>"
			+ "</homedroid>";
		return verxml;
	}

	public String loginUser(User user) throws IOException {
		String userxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<loginUser>"
					+ "<username>" + user.getUsername()	+ "</username>"
					+ "<password>" + user.getPassword()	+ "</password>"
				+ "</loginUser>"
			+ "</homedroid>";
		return userxml;
	}

	public String regUser(User user) throws IOException {
		String userxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<regUser>"
					+ "<username>" + user.getUsername()	+ "</username>"
					+ "<password>" + user.getPassword()	+ "</password>"
				+ "</regUser>"
			+ "</homedroid>";
		return userxml;
	}

	public String sendBarcode(String barcode) throws IOException {
		String barcodexml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<sendBarcode>"
					+ "<barcode>" + barcode + "</barcode>"
				+ "</sendBarcode>"
			+ "</homedroid>";
		return barcodexml;
	}

	public String sendList(ArrayList<Product> templist, String username, String password) throws IOException {
		String[] stringlist = new String[templist.size()];
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < templist.size(); i++) {
			stringlist[i] = "<item>" + i + "</item>"
				+ "<amount>" + templist.get(i).getAmount() + "</amount>"
				+ "<barcode>" + templist.get(i).getBarcode() + "</barcode>";
			buffer.append(stringlist[i]);
		}

		String productxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<sendList>" + buffer
					+ "<username>" + username + "</username>"
					+ "<password>" + password + "</password>"
				+ "</sendList>"
			+ "</homedroid>";
		return productxml;

	}
	
	public String sendOutOfStock(ArrayList<Product> templist, String username, String password) {
		System.out.println("templist: " + templist.get(0).getBrand());
		String[] stringlist = new String[templist.size()];
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < templist.size(); i++) {
			stringlist[i] = "<item>" + i + "</item>"
				+ "<amount>" + templist.get(i).getAmount() + "</amount>"
				+ "<barcode>" + templist.get(i).getBarcode() + "</barcode>";
			buffer.append(stringlist[i]);
		}

		String outofstockxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<outOfStock>" + buffer
					+ "<username>" + username + "</username>"
					+ "<password>" + password + "</password>"
				+ "</outOfStock>"
			+ "</homedroid>";
		return outofstockxml;
	}

	public String getList(User user) {
		String sendlistxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<getList>"
					+ "<username>" + user.getUsername() + "</username>"
					+ "<password>" + user.getPassword()	+ "</password>"
				+ "</getList>"
			+ "</homedroid>";
		return sendlistxml;
	}

	public String addProduct(String barcode, String label, String brand, String content_, String content_2, int u_id) throws IOException {
		String addproductxml = "<?xml version=\"1.0\"?>"
			+ "<homedroid>"
				+ "<addProduct>"
					+ "<barcode>" + barcode + "</barcode>"
					+ "<label>" + label + "</label>"
					+ "<brand>" + brand	+ "</brand>"
					+ "<content_>" + content_ + "</content_>"
					+ "<content_2>" + content_2 + "</content_2>"
					+ "<u_id>" + u_id + "</u_id>"
				+ "</addProduct>"
			+ "</homedroid>";
		return addproductxml;

	}

	public String xmlread(String toEncode) {
		try {

			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			DefaultHandler handler = new DefaultHandler() {

				public void startElement(String uri, String localName,String qName, Attributes attributes)throws SAXException {

					if (localName.equalsIgnoreCase("acknowledge")) {
						auswahl = 1;
					}
					if (localName.equalsIgnoreCase("sendProductDescription")) {
						auswahl = 2;
					}
					if (localName.equalsIgnoreCase("getList")) {
						auswahl = 3;
					}
					if (localName.equalsIgnoreCase("checkUpdates")) {
						auswahl = 4;
					}
					if (localName.equalsIgnoreCase("getAlreadyOnStockList")) {
						auswahl = 3;
					}

					// ###########################################################################################

					if (localName.equalsIgnoreCase("version")) {
						version = true;
					}
					if (localName.equalsIgnoreCase("message")) {
						message = true;
					}
					if (localName.equalsIgnoreCase("amount")) {
						amount = true;
					}
					if (localName.equalsIgnoreCase("actual_amount")) {
						List1.product.addToOnStockList();
						actual_amount = true;
					}
					if (localName.equalsIgnoreCase("barcode")) {
						barcode = true;
					}
					if (localName.equalsIgnoreCase("label")) {
						label = true;
					}
					if (localName.equalsIgnoreCase("brand")) {
						brand = true;
					}
					if (localName.equalsIgnoreCase("content_")) {
						content_ = true;
					}
					if (localName.equalsIgnoreCase("content_2")) {
						content_2 = true;
					}
					if (localName.equalsIgnoreCase("unit")) {
						unit = true;
					}

				}

				public void characters(char ch[], int start, int length) throws SAXException {
					switch (auswahl) {
					case 1:

						if (message) {
							strmessage = new String(ch, start, length);
							message = false;
						}

						break;
					case 2:
						if (barcode) {
							List1.product.setBarcode(new String(ch, start,length));
							List2.product.setBarcode(new String(ch, start,length));
							barcode = false;
						}

						if (label) {
							List1.product.setLabel(new String(ch, start, length));
							List2.product.setLabel(new String(ch, start, length));
							label = false;
						}
						if (brand) {
							List1.product.setBrand(new String(ch, start, length));
							List2.product.setBrand(new String(ch, start, length));
							brand = false;
						}
						if (content_) {
							List1.product.setContent_(new String(ch, start,length));
							List2.product.setContent_(new String(ch, start,length));
							content_ = false;
						}
						if (content_2) {
							List1.product.setContent_2(new String(ch, start,length));
							List2.product.setContent_2(new String(ch, start,length));
							content_2 = false;
						}
						if (unit) {
							List1.product.setUnit(new String(ch, start, length));
							List2.product.setUnit(new String(ch, start, length));
							unit = false;
						}
					case 3:
						if (actual_amount) {
							List1.product.setActualAmount(new String(ch, start,length));
							actual_amount = false;
						}
						if (amount) {
							List1.product.setAmount(new String(ch, start,length));
							amount = false;
						}
						if (barcode) {
							List1.product.setBarcode(new String(ch, start,length));
							barcode = false;
						}
						if (label) {
							List1.product.setLabel(new String(ch, start, length));
							label = false;
						}
						if (brand) {
							List1.product.setBrand(new String(ch, start, length));
							brand = false;
						}
						if (content_) {
							List1.product.setContent_(new String(ch, start,length));
							content_ = false;
						}
						if (content_2) {
							List1.product.setContent_2(new String(ch, start,length));
							content_2 = false;
						}
						if (unit) {
							List1.product.setUnit(new String(ch, start, length));
							unit = false;
						}

						break;
						
					case 4:
						if (version){
							strmessage = new String(ch, start, length);
							version = false;
						}

					default:
						break;
					}
				}

				@Override
				public void endDocument() throws SAXException {
					super.endDocument();
				}

				@Override
				public void endElement(String uri, String localName,
						String qName) throws SAXException {
					super.endElement(uri, localName, qName);
				}

				@Override
				public void startDocument() throws SAXException {
					super.startDocument();
				}

			};
			StringReader inStream = new StringReader(toEncode);
			InputSource inSource = new InputSource(inStream);
			saxParser.parse(inSource, handler);
		} catch (Exception e) {
			System.out.println("debug-parser");
			e.printStackTrace();
		}
		return strmessage;
	}


}
